﻿#include "idlestate.h"
#include "common_log.h"
#include "component_data.h"

IdleState::IdleState(QObject *parent)
	: SystemStateBase(parent)
{
	//connect(m_com, &ComponentManager::PublishSystemEvent, this, &SystemStateBase::ComponentEvnetHandle);
}

IdleState::~IdleState()
{
}

bool IdleState::InitState()
{
	stopFlag = true;
	return true;
}

bool IdleState::Running()
{
	log_trace("***************SYSTEM STATE IDLE***************");
	stopFlag = false;
	m_writer->WriteTraceJournal(u8"进入空闲状态");
	//bool ret = m_com->CheckComponent();
	//if (ret) {
	//	m_writer->WriteTraceJournal(u8"组件检查完成，等待启动操作");
	//	return true;
	//} else {
	//	m_writer->WriteWarnJournal(u8"组件检查异常，无法启动。切换到停止状态");
	//	return false;
	//}
	return true;
}

void IdleState::ComponentEvnetHandle(int event)
{
	if (stopFlag) {
		return;
	}
	// 启动按钮 切换到工作模式
	if ((int)ComponentEvent::system_event_start == event) {
		m_writer->WriteTraceJournal("收到启动命令，开始切换状态");
		emit ChangeSystemState(2);
		return;
	}
	if ((int)ComponentEvent::system_event_reset == event) {
		m_writer->WriteTraceJournal("空闲模式下，无法执行复位操作");
		return;
	}
	if ((int)ComponentEvent::system_event_stop == event) {
		QString str = m_com->GetErrorMsg();
		m_writer->WriteWarnJournal(QString(u8"异常：%1, 切换到停止模式").arg(str));
	}
	emit ChangeSystemState(3);
}
